<?php
// +----------------------------------------------------------------------
// | [RhaPHP System] Copyright (c) 2017 http://www.rhaphp.com/
// +----------------------------------------------------------------------
// | [RhaPHP] 并不是自由软件,你可免费使用,未经许可不能去掉RhaPHP相关版权
// +----------------------------------------------------------------------
// | Author: Geeson <qimengkeji@vip.qq.com>
// +----------------------------------------------------------------------


namespace addons\hlapp\model;

use think\Db;
use think\Log;
use think\Model;
use addons\hlapp\dto\PageInfo;
use addons\hlapp\dto\Result;

class HlappProject extends CommonModel
{

    //推送状态获取器
    public function getIsPushAttr($value){
        $isPushArr = ['0'=>"未推送",'1'=>"已推送"];
        return $isPushArr[$value];
    }
    //状态获取器
    public function getStatusAttr($value)
    {
        $status = [0=>'未开始',1=>'进行中',2=>'结束'];
        return $status[$value];
    }
    // 阶段获取器
    public function getStageAttr($value)
    {
        if($value){
            $typeStageList = HlappTypeStage::getTypeStage();
            $stageObj = $typeStageList[$value];
            return $stageObj['name'];
        }else{
            return null;
        }
    }
    //项目种类获取器
    public function getTypeAttr($value){
        $types = HlappProjectType::getProjectType();
        $type=$types[$value];
        return $type["name"]."(".$type["parent"].")";
    }
    //开始日期获取器
    public function getStartTimeAttr($value){
        return $this->dateStampFormat($value);
    }
    //截止日期获取器
    public function getEndTimeAttr($value){
        return $this->dateStampFormat($value);
    }
    //截止日期修改器 不执行？
//    public function setEndTimeAttr($value){
//        Log::info('查看看修改器');
//        return strtotime($value);
//    }
    //关联关系
    public function rolesMember()
    {
        return $this->hasMany('HlappProjectRolesMember','p_id');
    }
    public function amamager(){ return $this->hasOne('HlappMember','id','amamager_id');}
    public function aengineer(){ return $this->hasOne('HlappMember','id','aengineer_id');}
    public function manager(){ return $this->hasOne('HlappMember','id','manager_id');}
    public function engineer(){ return $this->hasOne('HlappMember','id','engineer_id');}

    /**
     * 获取项目
     * @param $id
     * @return array|null|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getProject($id){
        $project = (new HlappProject())->with('amamager,aengineer,manager,engineer')->find($id);
        return $project;
    }

    /**
     * 评价列表
     */
    public function evaluateList($pId){
        $mode = new HlappProject();
        $sqlList = " select e.*,s.description,
                      from hl_hlapp_project_evaluate e
                      left JOIN hl_hlapp_project_status s ON s.id = e.statusId
                      WHERE e.del = 0 AND e.projectId=".$pId;
        $list = $mode->query($sqlList);
        return $list;
    }

    //保存
    public function addOrUpdateOpenid($phone,$openId){
        $this->where("chargePhone",$phone)->setField("openId",$openId);
        $this->update();
        return true;
    }

    /**
     * 添加项目
     * @return string
     */
    public function addProject(){
        $project = $this->where("name",$_POST["name"])->select();
        if(count($project)>0){
            return Result::getError("项目已存在");
        }else{
            $hlProject = new HlappProject($_POST);
            $hlProject->allowField(true)->save();
            return Result::getOk($hlProject->getLastInsID());
        }
    }

    /**
     * 获取过期的项目
     */
    public function getOutOfDateProject(){
        $projects = $this->where('end_time','<',time())->order('id','desc')->select();
        return $projects;
    }

    /**
     * 不满意 一周内 无人 上传处理措施的项目
     */
//    public function getNoSolveProject(){
//        $evaluates =(new HlappProjectEvaluate())
//            ->where('create_time','>',strtotime("-7 day"))
//            ->where('status',0)->select();
//    }

    /**
     * 不满意次数超过 times 次的 项目
     */
    public  function moreBadTimes($times){
        $evaluates =(new HlappProjectEvaluate())
            ->field('projectId,count(*) times')
            ->where('status',0)
            ->group('projectId')
            ->select();
        $pIds =array();
        foreach ($evaluates as $evaluate){
            if ($evaluate['times'] >= $times){
                array_push($pIds,$evaluate['projectId']);
            }
        }
        return HlappProject::select($pIds);
    }
    private function dateStampFormat($time,$format='Y-m-d'){
        if ($time){
            $date = date($format,$time);
        }else{
            $date = "无";
        }
        return $date;
    }

}